package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.Md5Util;
import com.hmall.common.ThreadLocalUtil;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.ResultDTO;
import com.hmall.common.pojo.Item;
import com.hmall.common.pojo.User;
import com.hmall.feign.FeignClient;
import com.hmall.feign.FeignClient2;
import com.hmall.order.mapper.OrderDetailsMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.OrderDetailsService;
import com.hmall.order.service.OrderLogisticsService;
import com.hmall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private FeignClient feignClient;
    @Autowired
    private FeignClient2 feignClient2;
    @Autowired
    private OrderDetailsService orderDetailsService;
    @Autowired
    private OrderLogisticsService orderLogisticsService;

    /**
     * 提交订单
     *
     * @param params
     * @return
     */
    @Override
    public Long createOrder(RequestParams params) {
        Order order = new Order();
        //根据itemId查询商品信息
        Item item = feignClient.selectItemById(params.getItemId());
        //计算商品总价
        Long price = item.getPrice();
        Integer num1 = params.getNum();
        Long totalFee = price * num1;

        //将Order写入数据库tb_order表中
        order.setTotalFee(totalFee);
        Integer paymentType = params.getPaymentType();
        order.setPaymentType(paymentType);
        Long userId = ThreadLocalUtil.getUserId();
        order.setUserId(userId);
        order.setStatus(1);
        this.save(order);


        //将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setItemId(params.getItemId());
        orderDetail.setOrderId(order.getId());
        orderDetail.setNum(params.getNum());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetail.setName(item.getName());

        orderDetailsService.save(orderDetail);


        //将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();

        Integer addressId = params.getAddressId();
        Address address = feignClient2.findAddressByAddressId(addressId.toString());

        orderLogistics.setContact(address.getContact());

        orderLogistics.setOrderId(order.getId());

        orderLogistics.setMobile(address.getMobile());

        orderLogistics.setProvince(address.getProvince());

        orderLogistics.setCity(address.getCity());

        orderLogistics.setTown(address.getTown());

        orderLogistics.setStreet(address.getStreet());

        orderLogisticsService.save(orderLogistics);

        Integer num = params.getNum();
        Long itemId = params.getItemId();
        feignClient.updateStock(itemId, num);

        return order.getId();

    }

    /**
     * 模拟支付功能
     *
     * @param orderId
     * @param password
     * @return
     */
    @Override
    public ResultDTO pay(Long orderId, String password) {
        //根据订单id查询订单信息
        Order order = orderMapper.findOrderByOrderId(orderId);
//        System.out.println(orderId+55555);
        Integer status = order.getStatus();
        if (status == 1) {
            Long userId = order.getUserId();
            User user = feignClient2.findUserByUserId(userId);
            String password1 = user.getPassword();
            if (password1.equals(Md5Util.encodeByMd5(password))) {
                feignClient2.updateBalance(order.getTotalFee());
                orderMapper.updateOrderStateByOrderId(2,orderId);
            }
            return new ResultDTO(true,"支付成功",order);
        } else {
            throw new RuntimeException();
        }
    }

    @Override
    public Order findOrderByOrderId(Long orderId) {
        Order order = orderMapper.findOrderByOrderId(orderId);
        return order;
    }
}

